package reflect.demo;

import java.lang.reflect.Constructor;

/**
 * 操作特定构造器进行对象实例化
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("reflect.demo.Person");

//        Constructor construtor = cls.getConstructor();//获取无参构造器
//        construtor.newInstance();

        //Person(String name)
        Constructor constructor = cls.getConstructor(String.class);
        Object obj = constructor.newInstance("李四");//new Person("李四");
        System.out.println(obj);

        //Person(String name,int age)
        Constructor constructor2 = cls.getConstructor(String.class,int.class);
        Object obj2 = constructor2.newInstance("王五",33);//new Person("王五",33);
        System.out.println(obj2);
    }
}






